<?php
// +----------------------------------------------------------------------
// | Wendasns [ 开源问答社区系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020~2021 http://wendasns.com All rights reserved.
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\wendasns\controller;

use app\wendasns\WenBaseController;
use app\wendasns\service\Srv;
use app\wendasns\model\User;
use app\wendasns\model\Answer;
use app\wendasns\model\Question;
use app\wendasns\model\Article;
use app\wendasns\model\Fans;
use app\wendasns\service\PwSeo;
use think\facade\View;

class Home extends WenBaseController
{
    public function index($id=0, $dispatch='index', $page=1)
    {
    	$user = User::find($id);
    	if(!$user){
    		error('用户不存在');
    	}
    	
    	$list = [];
    	$limit = 10;
    	$paginate = ['list_rows'=>$limit,'page'=>$page,'query'=>['page'=>$page,'dispatch'=>$dispatch,'id'=>$id,'_s'=>'wendasns/home/index']];
    	switch ($dispatch) {
    		case 'answer':
    			$list = Answer::where('user_id',$id)->order('create_time', 'desc')->paginate($paginate);
    			break;
    		case 'article':
    			$list = Article::where('user_id',$id)->order('create_time', 'desc')->paginate($paginate);
    			break;
    		default:
    			$list = Question::where('user_id',$id)->order('create_time', 'desc')->paginate($paginate);
    	}
    	
    	$fans = Fans::where('user_id',$this->loginUser->uid)->where('target_id',$user->id)->find();
    	
		$seo = new PwSeo();
		$seo->title = $user->username.' - 主页';
		$seo->description = $user->members->usersign;
		$seo->keywords = $user->username;
		$seo->set();
		$data = ['user'=>$user,'dispatch'=>$dispatch,'logs'=>$list,'fans'=>$fans];
		View::assign($data);
		event('HomePage');
        return View::fetch();
    }
    
}